/**
 * 选项卡
 */
const multiTab = {
    state: {
        multiTab: true, // 是否开启选项卡功能
        activeKey: "", // 当前选中页
        cachedViews: [], // 选项卡缓存信息
    },
    mutations: {
        SET_ACTIVEKEY(state, activeKey) {
            state.activeKey = activeKey
        },
        ADD_CACHED_VIEW: (state, cache) => {
            if (state.cachedViews.some(c => c.key === cache.key)) return
            state.cachedViews.push(cache)
        },
        DEL_CACHED_VIEW: (state, cache) => {
            const index = state.cachedViews.findIndex(c => c.key === cache.key)
            index > -1 && state.cachedViews.splice(index, 1)
        },
        DEL_ALL_CACHED_VIEWS: state => {
            state.cachedViews = []
        }
    },
    actions: {
        SET_ACTIVEKEY({ commit }, activeKey) {
            commit('SET_ACTIVEKEY', activeKey)
        },
        ADD_CACHED_VIEW({ commit }, cache) {
            commit('ADD_CACHED_VIEW', cache)
        },
        /**
         * 删除选项卡缓存
         * @param {*} param0 
         * @param {*} path 
         * @returns 
         */
        DEL_CACHED_VIEW({ commit, state }, cache) {
            return new Promise(resolve => {
                commit('DEL_CACHED_VIEW', cache)
                resolve([...state.cachedViews])
            })
        }
    }
}

export default multiTab